﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager.Customer
{
    public partial class Creceive : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                //inputDateFrom.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                //inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();

            }
        }

        #region GV Bind
        private void GVDataBind()
        {

            string strSQL = "SELECT a.destaddr,a.srcaddr,a.msg,b.ChannelPrice,count(a.srcaddr) as Rnum FROM T_Receive a,T_Channel b WITH(NOLOCK) WHERE a.destaddr=b.ChannelNumber AND a.msg like '*'+b.ChannelOrder+'*' ";

            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(txtsrcaddr.Text.Trim()))
                strWhere += string.Format(" AND a.srcaddr LIKE '%{0}%'", txtsrcaddr.Text.Trim());
            if (!string.IsNullOrEmpty(txtdestaddr.Text.Trim()))
                strWhere += string.Format(" AND a.destaddr LIKE '%{0}%'", txtdestaddr.Text.Trim());
            //if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
            //    strWhere += string.Format(" AND a.CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            strSQL += strWhere;

            strSQL += " GROUP BY a.destaddr,a.srcaddr,a.msg,b.ChannelPrice ";

            strSQL += string.Format(" ORDER BY a.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

            //object o = DbHelperSql.GetSingle("SELECT count(*) FROM T_Receive a,T_Channel b WHERE a.destaddr=b.ChannelNumber AND a.msg like '*'+b.ChannelOrder+'*' " + strWhere);
            //if (o != null)
            //{
            //    lbRnumAll.Visible = true;
            //    lbRnumAll.Text = "总条数：" + o.ToString();
            //}
            //else lbRnumAll.Visible = false;
        }
        private void GVDataBindAll()
        {
            string strSQL = "SELECT a.destaddr,a.srcaddr,a.msg,b.ChannelPrice,count(a.srcaddr) as Rnum,a.destaddr+';'+a.srcaddr+';'+a.msg as AllArg FROM T_Receive a,T_Channel b WITH(NOLOCK) WHERE a.destaddr=b.ChannelNumber AND a.msg like '*'+b.ChannelOrder+'*' ";

            strSQL += " GROUP BY a.destaddr,a.srcaddr,a.msg,b.ChannelPrice ";

            strSQL += string.Format(" ORDER BY a.{0} {1}", ddlOrder.SelectedValue, ddlAD.SelectedValue);
            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;

            //object o = DbHelperSql.GetSingle("SELECT count(*) FROM T_Receive a,T_Channel b WHERE a.destaddr=b.ChannelNumber AND a.msg like '*'+b.ChannelOrder+'*' ");
            //if (o != null)
            //{
            //    lbRnumAll.Visible = true;
            //    lbRnumAll.Text = "总条数：" + o.ToString();
            //}
            //else lbRnumAll.Visible = false;
        }
        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                //{
                //    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                //    {
                //        MM.Common.Common.PageStartAlert(this.Page, "创建时间From必须小于等于To！");
                //        return;
                //    }
                //}
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }
    }
}
